<style type="text/css">
    .selected-search {
        box-shadow: 0 1px 6px rgba(0, 0, 0, .2);
        height: 200px;
        width: 260px;
        position: relative;
        border-radius: 4px;
    }

    .selected-search .search-action {
        position: absolute;
        top: 10px;
        left: 10px;
        right: 10px;
    }

    .selected-search .selected-arr {
        position: absolute;
        top: 50px;
        left: 20px;
        right: 20px;
        bottom: 10px;
        overflow: scroll;
    }

    .select-click {
        border: 1px solid #dddee1;
        border-radius: 4px;
        padding: 0 10px;
        margin-bottom: 10px;
    }

    .selected-search .selected-arr .selected-item {
        /*border-bottom: 1px solid #dfdfdf;*/
    }
</style>
<template>
    <i-form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
        <Form-item label="组织名称" prop="title">
            <i-input v-model="formValidate.title" placeholder="请输入组织名称"></i-input>
        </Form-item>
        <Form-item label="负责人" prop="personalName">
            <!--<i-input  :aria-disabled="true" placeholder="选择负责人" v-model="personalName"></i-input>-->
            <p @click="getPeopelData()" class="select-click">{{personalName}}</p>
            <div v-if="selectPeopelIsShow" class="selected-search">
                <div class="search-action">
                    <i-input placeholder="查询人姓名"></i-input>
                </div>
                <div class="selected-arr">
                    <template v-for="(item,index) in principalArr">
                        <p @click="choosePeople(item.Id,item.Personalname)" class="selected-item">
                            {{item.Personalname}}</p>
                    </template>
                </div>
            </div>
        </Form-item>
        <!--</Form-item>-->
        <Form-item>
            <i-button @click="handleSubmit('formValidate')" type="primary">提交</i-button>
            <i-button type="ghost" style="margin-left: 8px">取消</i-button>
        </Form-item>
    </i-form>

</template>
<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                formValidate: {
                    //添加字段
                    title: '',
                },
                ruleValidate: {
                    title: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ],
                },
                image: '',
                principalId: '',
                principalArr: [],
                personalName: '请选择负责人',
                selectPeopelIsShow: false,
            }
        },
        mounted: function () {
            let id = this.$route.query.id;

            if (id && id.length > 0) {

                this.getDetail(id)
            }

        },

        methods: {
            changeOutDate: function (date) {
                console.log(date);
                this.formValidate.outdate = date;
            },
            handleSubmit(name) {

                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.updateData(model);
                })
            },
            //获取负责人
            getPeopelData() {
                let that = this;

                let para = {
                    curPage: 1,
                    pageSize: '',
                };

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.userList, para, function (res) {
                    that.$Loading.finish();

                    that.selectPeopelIsShow = true;
                    that.principalArr = res.Msg.Arrays;
                });
            },
            //详情
            getDetail: function (id) {
                let that = this;
                that.$Loading.start();

                let para = {
                    id: id,
                };

                apiUtil.get(that, apiUtil.urls.OrganizeDetail, para, function (res) {
                    that.$Loading.finish();
                    that.formValidate.title = res.Msg.Title;
                    that.uplevelid = res.Msg.UplevelId;
                    that.principalId = res.Msg.Principalid;
                    if (res.Msg.Personalname) {
                        that.personalName = res.Msg.Personalname;
                    } else {
                        that.personalName = '请选择负责人';
                    }

                });
            },
            //修改
            updateData: function (model) {
                let that = this;

                model.id = this.$route.query.id;
                model.principalId = this.principalId;
                model.faceimg = this.imagePost[0];
                model.uplevelid = this.uplevelid;

                let params = that.$qs.stringify(model);

                this.$Loading.start();

                apiUtil.post(that, apiUtil.urls.OrganizeEdit, params, function (res) {
                    that.$Loading.finish();
                    that.$message.success('编辑成功')
                });
            },

            //预览上传图片
            handleView(url) {
                this.seeImg = url;
                this.visible = true;
            },
            //删除图片
            handleRemove(file) {
                // 从 upload 实例删除数据
                let that = this;

                that.imagePost = [];

                const fileList = this.$refs.upload.fileList;
                this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
                this.$refs.upload.fileList.forEach(function (item, i) {
                    that.imagePost.push(that.apiUtilBase + item.response.Msg);
                });
                console.log('删除后:', this.imagePost)
            },
            //上传成功
            handleSuccess(res, file) {
                let that = this;
                that.imagePost = [];
                file.url = file.response.data;
                console.log('this.$refs.upload:', this.$refs.upload.fileList);
                this.$refs.upload.fileList.forEach(function (item, i) {
                    that.imagePost.push(that.apiUtilBase + item.response.Msg);
                });

            },
            //上传成功
            handleFormatError(file) {
                this.$Notice.warning({
                    title: '文件格式不正确',
                    desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
                });
            },
            handleMaxSize(file) {
                this.$Notice.warning({
                    title: '超出文件大小限制',
                    desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
                });
            },
            handleBeforeUpload() {
                const check = this.imageList.length < 1;
                if (!check) {
                    this.$Notice.warning({
                        title: '最多只能上传1张图片。'
                    });
                }
                return check;
            }
        }
    }
</script>
